import java.util.List;

public class MyLinkedList implements IList {

    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    /**
     * 打印链表
     * 跟单链表一样
     * 遍历链表 从头走到尾
     */
    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 统计链表的个数
     *
     * @return
     */
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 判断链表是否有 key 值
     *
     * @param key
     * @return
     */
    @Override
    public boolean contains(int key) {
        if (head == null) {
            return false;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 头插法
     * 头插节点之前要实例化该节点
     *
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //当链表为空
        //此时头节点为空 没有前驱
        //而 实例化的数据有前驱
        if (head == null) {
            head = node;
            last = node;
        } else {
            //任何数据插入，都要先绑后面
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    /**
     * 尾插法
     *
     * @param data
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            //last 向后走
            last = node;
        }
    }

    /**
     * 在指定位置 插入数据
     *
     * @param index
     * @param data
     */
    @Override
    public void addIndex(int index, int data) {

        //首先先检查index位置是否合法
        if (index < 0 || index > this.size()) {
            throw new IndexException("双向链表插入index位置不合法" + index);
        }
        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == this.size()) {
            addLast(data);
            return;
        }

        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        //中间位置插入
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }


    /**
     * 删除 一个key
     *
     * @param key
     */
    @Override
    public void remove(int key) {
        //双向链表删除谁，就找到谁
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        //只有一个节点,还是删除的节点
                        last = null;
                    }
                } else {
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    } else {
                        //删除尾巴结点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }

    }

    /**
     * 删除所有的 key
     *
     * @param key
     */
    @Override
    public void removeAllKey(int key) {
        //双向链表删除谁，就找到谁
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        //只有一个节点,还是删除的节点
                        last = null;
                    }
                } else {
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    } else {
                        //删除尾巴结点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }


    @Override
    public void clear() {
        this.head = null;
        this.last = null;
    }


}


